package com.gitee.wsl.android.notification

//package com.ivy.notifications

import android.Manifest
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.pm.PackageManager
import android.graphics.Color
import androidx.core.app.NotificationCompat
import androidx.core.content.ContextCompat
import com.gitee.wsl.android.ext.ApplicationInit
import timber.log.Timber

val notificationManager by lazy{
    ApplicationInit.application.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
}

class NotificationService(
    private val context: Context
) {

    fun defaultIvyNotification(
        channel: IvyNotificationChannel,
        id:Int,
        autoCancel: Boolean = true,
        priority: Int = NotificationCompat.PRIORITY_HIGH,
        smallIconId:Int
    ): IvyNotification {
        val ivyNotification = IvyNotification(context, id,channel)
        val color =  Color.GREEN
        ivyNotification.setSmallIcon(smallIconId)
            .setColor(color)
            .setPriority(priority)
            .setColorized(true)
            .setLights(color, 1000, 200)
            .setAutoCancel(autoCancel)
        return ivyNotification
    }


    fun showNotification(
        notification: IvyNotification,
        notificationId: Int = notification.id
    ) {
        // Register the channel with the system
        val channel = notification.ivyChannel.create()
        showNotification(notificationId,notification,channel)
    }

    fun showNotification(
        notificationId: Int,
        notification: IvyNotification,
        notificationChannel: NotificationChannel
    ){
        if (ContextCompat.checkSelfPermission(notification.context, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
            Timber.d("notification permission un granted.")
            return
        }

        //createNotificationChannel(notificationChannel)

        notificationManager.notify(notificationId, notification.build())
    }

    fun dismissNotification(notificationId: Int) {

        notificationManager.cancel(notificationId)
    }

    /**
     * 创建通知渠道
     */
    fun createNotificationChannel(notificationChannel: NotificationChannel) =
        notificationManager.createNotificationChannel(notificationChannel)

    /**
     * 创建横幅通知
     */
    fun createBannerNotificationChannel(notificationChannel: NotificationChannel) =
        notificationManager.createNotificationChannel(
            notificationChannel.apply {
                description = "提醒式通知"//渠道描述
                enableLights(true)//开启闪光灯
                lightColor = Color.BLUE//设置闪光灯颜色
                enableVibration(true)//开启震动
                vibrationPattern = longArrayOf(0, 1000, 500, 1000)//震动模式
                setSound(null, null)//没有提示音
            }
        )
}